package com.ajhlp.spring_modules.web.controller;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/file")
public class FileUploadController {
	
	private Logger log = Logger.getLogger(getClass());
	
	private String progressResultParamName = "progress";
	
	
	public String getProgressResultParamName() {
		return progressResultParamName;
	}

	public void setProgressResultParamName(String progressResultParamName) {
		this.progressResultParamName = progressResultParamName;
	}
	
	@RequestMapping(method=RequestMethod.GET,value={"/","/list"})
	public String list(HttpServletRequest request, HttpServletResponse response){
		return "upload";
	}
	

	@RequestMapping(value="/upload")
	public void upload(HttpServletRequest request, HttpServletResponse response){
		
		response.setCharacterEncoding("UTF-8");
		
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if(!isMultipart){
			log.debug("请求不包含上传文件");
			try {
				response.getWriter().write("0");
				response.getWriter().flush();
				response.getWriter().close();
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
		}
		
		FileItemFactory factory = new DiskFileItemFactory();

		// Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload(factory);
		
//		upload.setProgressListener(new FileUploadProgressListener(request.getSession(true)));

		// Parse the request
		try {
			@SuppressWarnings("unchecked")
			List<FileItem> items = upload.parseRequest(request);
			
			Iterator<FileItem> item = items.iterator();
			
			while (item.hasNext()) {
			    FileItem tmp = item.next();

			    if (tmp.isFormField()) {
			        this.processFormField(tmp);
			    } else {
			        this.processUploadedFile(tmp);
			    }
			}
			
			try {
				response.getWriter().write("1");
				response.getWriter().flush();
				response.getWriter().close();
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			try {
				response.getWriter().write("0");
				response.getWriter().flush();
				response.getWriter().close();
				return;
			} catch (IOException e1) {
				// TODO Auto-generated catch block
			}
		}
	}
	
	private void processUploadedFile(FileItem tmp) {
		// TODO Auto-generated method stub
		log.debug("[filename]"+tmp.getName()+"[size]"+tmp.getSize());
	}

	private void processFormField(FileItem tmp) {
		// TODO Auto-generated method stub
		log.debug("[param:"+tmp.getFieldName()+"]"+tmp.getString());
	}
	
}
